package com.zq.sysuser.controller;


import com.github.pagehelper.PageInfo;
import com.zq.common.logs.OperatorType;
import com.zq.common.logs.sysLogs;
import com.zq.common.util.Result;
import com.zq.sysuser.dao.dto.AssginMenuDto;
import com.zq.sysuser.dao.dto.UserRoleDto;
import com.zq.sysuser.dao.entity.Role;
import com.zq.sysuser.service.IRolePermissionService;
import com.zq.sysuser.service.IRoleService;
import jakarta.annotation.Resource;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张乔
 * @since 2024-04-26
 */
@RestController
@RequestMapping("/sys/role")
@Slf4j
public class RoleController {

@Resource
private IRoleService sysRoleService ;

//查询角色列表，分页查询。num、size和角色名
    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo<Role>> findByPage(@RequestParam("roleName") String roleName,
                                             @PathVariable(value = "pageNum") Integer pageNum ,
                                             @PathVariable(value = "pageSize") Integer pageSize) {
        PageInfo<Role> pageInfo = sysRoleService.findByPage(roleName , pageNum , pageSize) ;
        return Result.successData(pageInfo) ;
    }

//    新增角色
    @PostMapping("/add")
    @sysLogs(title = "角色管理：新增角色",businessType = 1,operatorType = OperatorType.MANAGE)
    public Result<String> add(@RequestBody Role role) {
        sysRoleService.addRole(role) ;
        return Result.success() ;
    }

//修改角色
@PostMapping("/update")
@sysLogs(title = "角色管理：修改角色",businessType = 1,operatorType = OperatorType.MANAGE)

public Result<String> update(@RequestBody Role role) {
    sysRoleService.updateRole(role);
    return Result.success();
}
//根据id删除角色
@sysLogs(title = "角色管理：删除角色",businessType = 1,operatorType = OperatorType.MANAGE)

@DeleteMapping("/deleted/{id}")
    public Result<String> delete(@PathVariable("id") Integer id) {
        sysRoleService.deleteById(id);
        return Result.success();
}


//查询所有角色
@GetMapping("/getAll")
public Result<List<Role>> getAll() {
    List<Role> roleList = sysRoleService.list();
    return Result.successData(roleList);

}






//给用户分配角色
    @PostMapping("/userRole")
    @sysLogs(title = "角色管理：给用户分配角色",businessType = 1,operatorType = OperatorType.MANAGE)

    public Result<String> doAssign(@RequestBody UserRoleDto userRoleDto) {
        sysRoleService.doAssign(userRoleDto) ;
        return Result.success();
    }

//用户拥有角色数据回显
@GetMapping(value = "/findAllRoles/{userId}")
public Result<Map<String , Object>> findAllRoles(@PathVariable(value = "userId") Integer userId) {
    Map<String, Object> resultMap   = sysRoleService.findAllRoles(userId);
    return Result.successData(resultMap);
}


@Resource
private IRolePermissionService rolePermissionService;


//查询所有菜单，并返回角色拥有的菜单
@GetMapping(value = "/findSysRoleMenuByRoleId/{roleId}")
public Result<Map<String , Object>> findSysRoleMenuByRoleId(@PathVariable(value = "roleId") Integer roleId) {
    Map<String , Object> sysRoleMenuList = rolePermissionService.findSysRoleMenuByRoleId(roleId) ;
    return Result.successData(sysRoleMenuList);
}

//给角色分配权限（或菜单）
    @PostMapping("/doAssign")
    @sysLogs(title = "角色管理：给角色分配权限",businessType = 1,operatorType = OperatorType.MANAGE)

    public Result<String> doAssign(@RequestBody AssginMenuDto assginMenuDto) {
    log.info("给角色分配权限（或菜单）"+assginMenuDto);
        rolePermissionService.doAssign(assginMenuDto);
        return Result.success();
    }







}
